//
//  ViewController.m
//  MovieList
//
//  Created by 黄威 on 16/6/21.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ViewController.h"
#import "MovieModel.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource >

@property (nonatomic,retain)UITableView *table;
@property (nonatomic,retain)NSMutableArray *ArrayOfData;
@end

@implementation ViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handData];
    
 [self createTableView];
    
}

- (void)config{

    
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
    
    self.ArrayOfData = [NSMutableArray array];
    
    self.navigationItem.title = @"电影列表";


}

- (void)handData{


    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@".txt"];
    
    NSData * data = [NSData dataWithContentsOfFile:path];
    
    
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *array = [dictionary objectForKey:@"result"];
    
    
    for (NSDictionary *dic in array) {
        
        
        MovieModel *model = [[MovieModel alloc]init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.ArrayOfData addObject:model];
        
        NSLog(@"%@",self.ArrayOfData);
        
    }
    
    
    

}


- (void)createTableView{


    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375,675) style:UITableViewStylePlain];
    
    
    
    
    self.table.delegate = self;
    self.table.dataSource = self;
    
    [self.view addSubview:self.table];
    
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return  1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return self.ArrayOfData.count;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    MovieModel *model = [self.ArrayOfData objectAtIndex:indexPath.row];
    
    
    NSURL *url = [[NSURL alloc]initWithString:model.pic_url];
    
    NSData *dataOfImage = [NSData dataWithContentsOfURL:url];
    
    UIImage *image = [UIImage imageWithData:dataOfImage];
    
    cell.textLabel.text = model.movieName;
    
    cell.imageView.image = image;
    
    NSLog(@"%@",cell.textLabel.text);
    
    
    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 120;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{

    [super setEditing:editing animated:YES];
    
    [self.table setEditing:editing animated:YES];

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

  
    return UITableViewCellEditingStyleDelete;

}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{


    if (editingStyle == UITableViewCellEditingStyleDelete ) {
        
        [self.ArrayOfData removeObjectAtIndex:indexPath.row];
        
       // [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
        
        [tableView reloadData];
        
    }
    

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
    
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{




}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
